import {
    createStore,
    applyMiddleware
} from "redux"
import thunk from "redux-thunk"
let initValue = {
    list:window.localStorage.getItem('state')?JSON.parse(window.localStorage.getItem('state')).list: [],
   allPrice: window.localStorage.getItem('state')?JSON.parse(window.localStorage.getItem('state')).allPrice: 0,
    allNum:window.localStorage.getItem('state')?JSON.parse(window.localStorage.getItem('state')).allNum: 0,
 //list:[],
  //allPrice:0,
 // allNum:0

}
let reducer = (state = initValue, action) => {


    switch (action.type) {
        case "SET_LIST":
            state.list = action.data;
            let allPrice=0;
            let allNum=0;
            action.data.forEach((item)=>{
              if(item.num){
                  allPrice+=item.num*item.price;
                  allNum+=item.num
              }
            });
            state.allPrice=allPrice;
            state.allNum=allNum;
            break;

        default:




    }
    window.localStorage.setItem('state',JSON.stringify(state));
    return JSON.parse(JSON.stringify(state))
}
export default createStore(reducer, applyMiddleware(thunk))